//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2006
//
// Change city dialog.
//

# include "StdAfx.h"
# include "Resource.h"
# include "ChangeCity.h"
# include "CitiesList.h"
# include "Dialog.h"

class CSelectCity : public CDialog {
  public:
	CSelectCity (bool _bIniSel, city_t * & _pCity) :
		CDialog (IDD_SELECT_CITY),
		m_List (false, true),
		m_bIniSel (_bIniSel),
		m_pCity (_pCity)
	{}

	// Override.
	virtual bool OnInit (HWND _hwndFocus);
	virtual void OnDestroy ();
	virtual void OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl);
	virtual bool OnNotify (int _idCtrl, NMHDR * _pInfo);

  private:
	CCitiesList m_List;
	const bool m_bIniSel;
	city_t * & m_pCity;
};

bool CSelectCity::OnInit (HWND _hwndFocus) {
	m_List.Init (m_hWnd, IDC_LIST);
	m_List.Activate (m_bIniSel, m_pCity);

	return true;
}

void CSelectCity::OnDestroy () {
	m_List.Destroy ();
}

void CSelectCity::OnCommand (WORD _wCmd, WORD _wNotifyCode, HWND _hwndCtl) {
	switch (_wCmd) {
		case IDOK:
			m_pCity = m_List.GetSelCity ();
			EndDialog (0);
			return;

		case IDCANCEL:
			EndDialog (1);
			return;
	}
}

bool CSelectCity::OnNotify (int _idCtrl, NMHDR * _pInfo) {
	switch (_pInfo->code) {
		case NM_CLICK:
		case LVN_KEYDOWN:
			if (_idCtrl == IDC_LIST) {
				EnableDlgItem (IDOK,      true);
				EnableDlgItem (IDC_APPLY, true);
			}
			break;

		case NM_RETURN:
		case NM_DBLCLK:
			if (_idCtrl == IDC_LIST)
				SendCommand (IDOK);
			break;

		default:
			if (m_List.OnParentNotify (_idCtrl, _pInfo, this))
				return true;
	}

	return CDialog::OnNotify (_idCtrl, _pInfo);
}

bool SelectCity (HWND _hWnd, bool _bIniSel, city_t * & _pCity) {
	CSelectCity scd (_bIniSel, _pCity);
	return scd.ShowModal (_hWnd) == 0;
}
